

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;

namespace JTC.MiniCommerce.Web.admin {
  public partial class notifications : System.Web.UI.Page {

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetNotificationsProperties();
        if (!Page.IsPostBack) {
          LoadNotifications();
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(notifications).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the ItemDataBound event of the dgNotifications control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
    void dgNotifications_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        HyperLink hlEdit = e.Item.Cells[0].FindControl("hlEditLink") as HyperLink;
        if(hlEdit != null) {
          hlEdit.Text = LocalizationUtility.GetText("lblEdit");
        }
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }
    }

    #endregion

    #region Methods

    #region Protected

    /// <summary>
    /// Formats the edit URL.
    /// </summary>
    /// <param name="notificationId">The notification id.</param>
    /// <returns></returns>
    protected string FormatEditUrl(string notificationId) {
      return string.Format("~/admin/notificationedit.aspx?notificationId={0}", notificationId);
    }

    #endregion

    #region Private

    /// <summary>
    /// Loads the notifications.
    /// </summary>
    private void LoadNotifications() {
      NotificationCollection notificationCollection = new NotificationController().FetchAll();
      if (notificationCollection.Count > 0) {
        dgNotifications.DataSource = notificationCollection;
        dgNotifications.ItemDataBound += new DataGridItemEventHandler(dgNotifications_ItemDataBound);
        dgNotifications.Columns[0].HeaderText = LocalizationUtility.GetText("hdrEdit");
        dgNotifications.Columns[1].HeaderText = LocalizationUtility.GetText("hdrName");
        dgNotifications.Columns[2].HeaderText = LocalizationUtility.GetText("hdrSystemNotification");
        dgNotifications.DataBind();
      }
    }

    /// <summary>
    /// Sets the notifications properties.
    /// </summary>
    private void SetNotificationsProperties() {
      LocalizationUtility.AddLabelText(lblNotificationList);
      this.Title = LocalizationUtility.GetText("titleNotifications");
    }

    #endregion

    #endregion

  }
}
